Validation = function(editor) {
	this.editor = editor;
	this.processEditPart = editor.getGraphicalViewer().getContents();
	this.processModel = this.processEditPart.getModel();
};

Validation.prototype = {
	validate : function() {
		return this.verifyEnd&&this.verifyStart()&&this.verifyModel()&& this.verifyAlone()&&this.verifyDuplicateNodeName()&&this.verifyDuplicateEdge();
	},

	// ========================================================================
	verifyModel : function() {
		var result = true;
		Gef.each(this.processModel.children, function(node) {
					if (typeof node.isValid == 'function') {
						result = node.isValid();
						if (result === false) {
							return false;
						}
					}
				});
		return result;
	},

	verifyStart : function() {
		var result = true;
		var count = 0;
		Gef.each(this.processModel.children, function(node) {
					if (node.getType() == 'start') {
						if (count > 0) {
							alert('cannot have more then one START.');
							result = false;
							return false;
						}
						if (node.getIncomingConnections().length != 0) {
							alert('START cannot have any incoming connections.');
							result = false;
							return false;
						}
						if (node.getOutgoingConnections().length != 1) {
							alert('START must have only one outgoing connection.');
							result = false;
							return false;
						}
						count++;
					}
				});
		if (count == 0) {
			if (Gef.notEmpty(Ext)) {
				Ext.Msg.alert('提示', '必须设置开始节点');
			} else {
				alert('必须设置开始节点');
			}
			result = false;
		}
		return result;
	},

	verifyEnd : function() {
		var count = 0;

		var result = true;
		Gef.each(this.processModel.children, function(node) {
					if (node.getType() == 'end') {
						count++;
						if (node.getOutgoingConnections().length != 0) {
							alert('END cannot have any outgoing connections.');
							result = false;
							return false;
						}
					}
				});
		if (result === false) {
			return false;
		}
		if (count == 0) {
			if (Gef.notEmpty(Ext)) {
				Ext.Msg.alert('提示', '必须设置结束节点');
			} else {
				alert('必须设置结束节点');
			}
			return false;
		}

		result = false;
		Gef.each(this.processModel.children, function(node) {
					if (node.getType() == 'end') {
						var ends = node.dom.getAttribute('ends');
						if (Gef.isEmpty(ends) || ends == 'processinstance') {
							result = true;
							return false;
						}
					}
				});
		if (result === false) {
			if (Gef.notEmpty(Ext)) {
				Ext.Msg.alert('提示', '不能将所有的结束节点的结束方式都设置为execution');
			} else {
				alert('不能将所有的结束节点的结束方式都设置为execution');
			}
		}
		return result;
	},

	verifyDuplicateNodeName : function() {
		var hasEmptyNodeName = false;

		var map = {};
		Gef.each(this.processModel.children, function(sourceNode) {
					var id = sourceNode.text;
					if (Gef.isEmpty(id)) {
						hasEmptyNodeName = true;
						return false;
					}
					if (typeof map[id] == 'undefined') {
						map[id] = 1;
					} else {
						map[id]++;
					}
				});
		if (hasEmptyNodeName) {
			if (Gef.notEmpty(Ext)) {
				Ext.Msg.alert('提示', '节点名称不能为空');
			} else {
				alert('节点名称不能为空');
			}
		}

		var info = '节点名称不能重复';
		for (var id in map) {
			var num = map[id];
			if (num > 1) {
				info += '\n ' + id;
			}
		}
		if (info != '节点名称不能重复') {
			if (Gef.notEmpty(Ext)) {
				Ext.Msg.alert('提示', info);
			} else {
				alert(info);
			}
			return false;
		} else {
			return true;
		}
	},

	verifyDuplicateEdge : function() {
		var hasDuplicatedEdgeName = false;
		var duplicatedEdgetName = null;
		var duplicatedNodeName = null;

		var map = {};
		Gef.each(this.processModel.children, function(sourceNode) {
					hasDuplicatedEdgeName = false;
					var edgeNames = [];

					Gef.each(sourceNode.getOutgoingConnections(), function(connection) {
								var connectionName = connection.text;
								if (Gef.isEmpty(connectionName)) {
									connectionName = '';
								}
								if (edgeNames.indexOf(connectionName) == -1) {
									edgeNames.push(connectionName);
								} else {
									hasDuplicatedEdgeName = true;
									duplicatedEdgeName = connectionName;
									duplicatedNodeName = sourceNode.text;
									return false;
								}

								var targetNode = connection.getTarget();
								var id = sourceNode.text + ' to ' + targetNode.text;
								if (typeof map[id] == 'undefined') {
									map[id] = 1;
								} else {
									map[id]++;
								}
							});

					if (hasDuplicatedEdgeName) {
						return false;
					}
				});

		if (hasDuplicatedEdgeName) {
			if (Gef.notEmpty(Ext)) {
				Ext.Msg.alert('提示', duplicatedNodeName + '存在重名的外向连线[' + duplicatedEdgeName + ']');
			} else {
				alert(duplicatedNodeName + '存在重名的外向连线[' + duplicatedEdgeName + ']');
			}
			return false;
		}

		var info = '连线不能重复';
		for (var id in map) {
			var num = map[id];
			if (num > 1) {
				info += '\n ' + id;
			}
		}
		if (info != '连线不能重复') {
			if (Gef.notEmpty(Ext)) {
				Ext.Msg.alert('提示', info);
			} else {
				alert(info);
			}
			return false;
		} else {
			return true;
		}
	},

	// ========================================================================
	verifyAlone : function() {
		var result = true;
		Gef.each(this.processModel.children, function(node) {
					if (node.getType() != 'start' && node.getOutgoingConnections().length == 0 && node.getIncomingConnections().length == 0) {
						result = false;
						return false;
					}
				});
		if (result === false) {
			alert('there is alone node');
			return false;
		}
		return true;
	}
};
